n <- 10000
gender <- rep(0:1, each = n/2)
y <- sample(0:10, n, replace = TRUE) + gender * sample(0:10, n, replace = TRUE)
x1 <- y + sample(0:10, n, replace = TRUE) + gender * sample(0:10, n, replace = TRUE)
x2 <- y + x1 + sample(0:10, n, replace = TRUE) + gender * sample(0:10, n, replace = TRUE)
dat <- data.frame(y = y, x1 = x1, x2 = x2, gender = gender)What’s that all about the contrasts
methods
stats
contrasts
Example dataset
Create a random dataset with criteria y, predictors x1 and x2 and gender.
Al variables are correlated.
Descriptives
psych::corr.test(dat)Call:psych::corr.test(x = dat)
Correlation matrix
y x1 x2 gender
y 1.00 0.81 0.87 0.55
x1 0.81 1.00 0.93 0.67
x2 0.87 0.93 1.00 0.72
gender 0.55 0.67 0.72 1.00
Sample Size
[1] 10000
Probability values (Entries above the diagonal are adjusted for multiple tests.)
y x1 x2 gender
y 0 0 0 0
x1 0 0 0 0
x2 0 0 0 0
gender 0 0 0 0
To see confidence intervals of the correlations, print with the short=FALSE option
psych::describe(dat)| vars | n | mean | sd | median | trimmed | mad | min | max | range | skew | kurtosis | se | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| y | 1 | 10000 | 7.4871 | 4.572672 | 7.0 | 7.27850 | 4.4478 | 0 | 20 | 20 | 0.3501775 | -0.4129928 | 0.0457267 |
| x1 | 2 | 10000 | 15.0661 | 7.392861 | 14.0 | 14.76500 | 7.4130 | 0 | 39 | 39 | 0.3575956 | -0.4521592 | 0.0739286 |
| x2 | 3 | 10000 | 30.0596 | 13.752671 | 28.0 | 29.50787 | 14.8260 | 0 | 74 | 74 | 0.3339310 | -0.5830752 | 0.1375267 |
| gender | 4 | 10000 | 0.5000 | 0.500025 | 0.5 | 0.50000 | 0.7413 | 0 | 1 | 1 | 0.0000000 | -2.0002000 | 0.0050003 |
Contrasts
#, contrasts = list(gender = contr.treatment(2))
# Gender has values 0 vs. 1 (treatment contrast)
fit1 <- lm(y ~ gender * x1 * x2, data = dat)
# Gender hast -1 vs. 1 (effect contrast)
dat$gender <- car::recode(dat$gender, "0 = -1; 1 = 1")
fit2 <- lm(y ~ gender * x1 * x2, data = dat)
sjPlot::tab_model(fit1, fit2, show.std = TRUE, show.ci = FALSE, col.order = c("est", "se", "std.est", "p"), digits = 4)| y | y | |||||
| Predictors | Estimates | std. Beta | p | Estimates | std. Beta | p |
| (Intercept) | -1.7456 | 0.0088 | <0.001 | -2.5621 | 0.0088 | <0.001 |
| gender | -1.6330 | -0.1693 | <0.001 | -0.8165 | -0.1693 | <0.001 |
| x1 | 0.0016 | -0.0039 | 0.941 | 0.0062 | -0.0039 | 0.657 |
| x2 | 0.3352 | 1.0002 | <0.001 | 0.3369 | 1.0002 | <0.001 |
| gender × x1 | 0.0092 | -0.0006 | 0.741 | 0.0046 | -0.0006 | 0.741 |
| gender × x2 | 0.0032 | -0.0027 | 0.830 | 0.0016 | -0.0027 | 0.830 |
| x1 × x2 | -0.0001 | -0.0064 | 0.881 | -0.0003 | -0.0064 | 0.518 |
| (gender × x1) × x2 | -0.0003 | -0.0037 | 0.707 | -0.0002 | -0.0037 | 0.707 |
| Observations | 10000 | 10000 | ||||
| R2 / R2 adjusted | 0.764 / 0.764 | 0.764 / 0.764 | ||||